﻿using Benefactor.Models.DtoModels;
using FluentValidation;

namespace Benefactor.Validations.DtoValidations;

public class IssueBenefitsCardThenAddOrderRequestDtoValidator: AbstractValidator<IssueBenefitsCardThenAddOrderRequestDto>
{
    public IssueBenefitsCardThenAddOrderRequestDtoValidator()
    {
        RuleFor(x => new
        {
            x.RefererAccount,
            x.UserId
        }).Must(x => x.RefererAccount != x.UserId)
        .WithErrorCode("The referer account cannot be the same with buyer account")
        .WithMessage("推荐人和购买人不能相同");

        RuleFor(x => new
            {
                x.RefererAccount,
                x.RefererFee
            }).Must(x => 
                (x.RefererAccount == null && x.RefererFee == null) ||
                (x.RefererAccount != null && x.RefererFee is > Statics.MinimumRefererFee))
            .WithErrorCode($"The referer account and referer fee must be both null or both have value and referer fee must be no less than {Statics.MinimumRefererFee}")
            .WithMessage($"推荐人和佣金必须同时为空或同时非空且佣金必须不少于{Statics.MinimumRefererFee}");

        RuleFor(x => new
            {
                x.RefererFee,
                x.PaidAmount
            }).Must(x =>
                x.RefererFee == null || x.RefererFee.Value < x.PaidAmount)
            .WithErrorCode("The referer fee must be less than the paid amount")
            .WithMessage("佣金必须少于支付金额");
    }
}